package com.yinhe.module.dr;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhSealInfo;
import com.yinhe.system.domain.YhSealMana;
import com.yinhe.system.service.IYhSealInfoService;
import com.yinhe.system.service.IYhSealManaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 印章管理Controller
 *
 * @author dr
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/dr/mana")
public class DrSealManaController extends BaseController
{
    @Autowired
    private IYhSealManaService yhSealManaService;
    @Autowired
    private IYhSealInfoService yhSealInfoService;
   @Autowired
   private HttpServletRequest autowiredRequest;
    /**
     * 查询印章管理列表
     */
    @PreAuthorize("@ss.hasPermi('dr:mana:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhSealMana yhSealMana)
    {
        startPage();
        List<YhSealMana> list = yhSealManaService.selectYhSealManaList(yhSealMana);
        return getDataTable(list);
    }

    /**
     * 导出印章管理列表
     */
    @PreAuthorize("@ss.hasPermi('dr:mana:export')")
    @Log(title = "印章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhSealMana yhSealMana)
    {
        List<YhSealMana> list = yhSealManaService.selectYhSealManaList(yhSealMana);
        ExcelUtil<YhSealMana> util = new ExcelUtil<YhSealMana>(YhSealMana.class);
        util.exportExcel(response, list, "印章管理数据");
    }

    /**
     * 获取印章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('dr:mana:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhSealManaService.selectYhSealManaById(id));
    }

    /**
     * 新增印章管理
     */
    @PreAuthorize("@ss.hasPermi('dr:mana:add')")
    @Log(title = "印章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhSealMana yhSealMana,YhSealInfo yhSealInfo) {
        String sn = yhSealMana.getSealNumber();
        String rs = yhSealMana.getReleaseStatus();
        List<YhSealInfo> list = yhSealInfoService.selectYhSealInfoList(yhSealInfo);

            for (int i = 0; i < list.size(); i++) {
                String sealNumber = list.get(i).getSealNumber();
                int z = Integer.valueOf(list.get(i).getSealNumber()).intValue();
                int n = Integer.valueOf(sn).intValue();
                if (z==n) {
                    YhSealInfo yhSealInfo1 = yhSealInfoService.selectYhSealInfoSealNumber(list.get(i).getSealNumber());
                    yhSealInfo1.setReleaseStatus(rs);
                    System.out.println(yhSealInfo1.getReleaseStatus());
                    yhSealInfoService.updateYhSealInfo(yhSealInfo1);
            }

        }
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhSealMana.setUserId(sysUser.getUserId());
        yhSealMana.setDeptId(sysUser.getDeptId());
        return toAjax(yhSealManaService.insertYhSealMana(yhSealMana));

    }

    /**
     * 修改印章管理
     */
    @PreAuthorize("@ss.hasPermi('dr:mana:edit')")
    @Log(title = "印章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhSealMana yhSealMana, YhSealInfo yhSealInfo)
    {
        String sn = yhSealMana.getSealNumber();
        String rs = yhSealMana.getReleaseStatus();
        List<YhSealInfo> list = yhSealInfoService.selectYhSealInfoList(yhSealInfo);

        for (int i = 0; i < list.size(); i++) {
            String sealNumber = list.get(i).getSealNumber();
            int z = Integer.valueOf(list.get(i).getSealNumber()).intValue();
            int n = Integer.valueOf(sn).intValue();
            if (z==n) {
                YhSealInfo yhSealInfo1 = yhSealInfoService.selectYhSealInfoSealNumber(list.get(i).getSealNumber());
                yhSealInfo1.setReleaseStatus(rs);
                System.out.println(yhSealInfo1.getReleaseStatus());
                yhSealInfoService.updateYhSealInfo(yhSealInfo1);
            }

        }

       /* List<YhSealInfo> list = yhSealInfoService.selectYhSealInfoList(yhSealInfo);
            for (int i = 0; i < list.size();i++){
                if (list.contains(sn)){
                    yhSealInfo.setReleaseStatus(sn);
                }
            }*/
        return toAjax(yhSealManaService.updateYhSealMana(yhSealMana));
    }

    /**
     * 删除印章管理
     */
    @PreAuthorize("@ss.hasPermi('dr:mana:remove')")
    @Log(title = "印章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhSealManaService.deleteYhSealManaByIds(ids));
    }
}
